const std = @import("std");
const expect = std.testing.expect;

const pipesLib = @import("../pipes.zig");


test "create and free" {
	var pipes = pipesLib.Pipes{
		.allocator = std.testing.allocator
	};
	pipes.init(10);
	defer pipes.deinit();

	try pipes.add_pipe(2, 2, pipesLib.PipeType.HORIZONTAL);
}


test "move pipe" {
	var pipes = pipesLib.Pipes{
		.allocator = std.testing.allocator
	};
	pipes.init(10);
	defer pipes.deinit();

	const old_x = 2;
	const old_y = 3;
	try pipes.add_pipe(old_x, old_y, pipesLib.PipeType.HORIZONTAL);
	const pipe = pipes.get_pipe(old_x, old_y).?;

	const new_x = 6;
	const new_y = 1;
	const new_pipe = try pipes.move_pipe(pipe, new_x, new_y);

	const old_pipe = pipes.get_pipe(old_x, old_y);

	try expect(new_pipe != null);
	try expect(old_pipe == null);
}


test "move untracked pipe" {
	var pipes = pipesLib.Pipes{
		.allocator = std.testing.allocator
	};
	pipes.init(10);
	defer pipes.deinit();

	const untracked_x = 1;
	const untracked_y = 2;
	var untracked_pipe = pipesLib.Pipe{
		.x = untracked_x,
		.y = untracked_y,
		.type = pipesLib.PipeType.VERTICAL
	};

	const move_x = 6;
	const move_y = 1;
	const moved_pipe = try pipes.move_pipe(&untracked_pipe, move_x, move_y);

	const fetched_untracked_pipe = pipes.get_pipe(untracked_x, untracked_y);

	try expect(moved_pipe == null);
	try expect(fetched_untracked_pipe == null);
}
